﻿using MTQ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using MTQ.Common;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.BLL
{
    public partial class Base_ButtonService : BaseService<Base_Button>
    {

        public List<MenuButton> GetAuthByMenu(List<Base_Menu> listmenu)
        {
            List<MenuButton> listMenubutton = new List<MenuButton>();


            List<Base_Button> listbuttonAll = this.LoadEntities(x => true).ToList();
            foreach (var menu in listmenu)
            {
                //listbutton.Where(x => x.MenuId == menu.Menu_Id).ToList();
                MenuButton mb = new MenuButton();
                mb.NodeId = menu.Menu_Id.ToString();
                mb.NodeName = menu.Menu_Name;

                if (menu.Items != null && menu.Items.Count > 0)
                {
                    List<MenuButton> listMenubuttonChildren = new List<MenuButton>();
                    foreach (var sunmenu in menu.Items)
                    {

                        MenuButton mbc = new MenuButton();
                        mbc.NodeId = sunmenu.Menu_Id.ToString();
                        mbc.NodeName = sunmenu.Menu_Name;

                        List<Button> buttons = new List<Button>();
                        List<Base_Button> listbutton = listbuttonAll.Where(x => x.Menu_Id == sunmenu.Menu_Id).ToList();
                        foreach (var button in listbutton)
                        {
                            buttons.Add(new Button() { Id = button.Button_Id.ToString(), Name = button.Button_Name });
                        }
                        mbc.Buttons = buttons;
                        listMenubuttonChildren.Add(mbc);
                    }

                    mb.Children = listMenubuttonChildren;
                }

                listMenubutton.Add(mb);
            }
            return listMenubutton;

        }

        public bool SaveAuth(Guid roleid, string buttonids)
        {
            try
            {
                List<Guid> listbuttonAll = this.LoadEntities(x => true).Select(x => x.Button_Id).ToList();
                List<Guid> listbuttonid = buttonids.Split(',').ToListGuid();

                List<Base_RoleButton> listBase_RoleButton = new Base_RoleButtonService().LoadEntities(x => x.Role_Id == roleid).ToList();
                //获取要删除的 
                List<Base_RoleButton> listBase_RoleButtonDelete = listBase_RoleButton.Where(x => !listbuttonid.Contains(x.Button_Id)).ToList();

                //获取要新增的
                List<Base_RoleButton> listBase_RoleButtonAdd = new List<Base_RoleButton>();
                List<Guid> listBase_RoleButtonExist = listBase_RoleButton.Select(x => x.Button_Id).ToList();
                List<Guid> listadd = listbuttonid.Where(x => listbuttonAll.Contains(x) && !listBase_RoleButtonExist.Contains(x)).ToList();
                foreach (var item in listadd)
                {
                    listBase_RoleButtonAdd.Add(new Base_RoleButton() { RoleButton_Id = System.Guid.NewGuid(), Role_Id = roleid, Button_Id = item });
                }
                Base_RoleButtonService rbs = new Base_RoleButtonService();
                rbs.AddListEntity(listBase_RoleButtonAdd);
                rbs.DeleteListEntity(listBase_RoleButtonDelete);

                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}
